package com.cy.platform;


import android.app.Dialog;
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.Window;
import android.view.WindowManager;

import java.util.List;

public class CapacityDialog {

    public interface ResultHandler {
        void handle(String time);
    }
    private ResultHandler handler;
    private Context mContext;
    private Dialog datePickerDialog;
    private RecyclerView rvList;
    private NumberAdapter numberAdapter;
    private List<String> sourceList;


    public CapacityDialog(Context context, ResultHandler handler, List<String> sourceList){
        this.mContext = context;
        this.handler = handler;
        this.sourceList = sourceList;
        initDialog();
        initView();
    }


    private void initDialog() {
        if (datePickerDialog == null) {
            datePickerDialog = new Dialog(mContext, R.style.DatePickerTheme);
            datePickerDialog.setCancelable(false);
            datePickerDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            datePickerDialog.setContentView(R.layout.layout_num_picker);
            Window window = datePickerDialog.getWindow();
//            window.setGravity(Gravity.BOTTOM);
            datePickerDialog.setCanceledOnTouchOutside(true);
            WindowManager manager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
            DisplayMetrics dm = new DisplayMetrics();
            manager.getDefaultDisplay().getMetrics(dm);
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = dm.widthPixels/3;
            lp.height = dm.heightPixels/2;
            window.setAttributes(lp);
        }
    }

    private void initView(){
        rvList = datePickerDialog.findViewById(R.id.rv_list);
        rvList.setLayoutManager(new LinearLayoutManager(mContext));
        numberAdapter = new NumberAdapter();
        rvList.setAdapter(numberAdapter);
        numberAdapter.setOnItemClickListener(new NumberAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(String item) {
                if(handler != null){
                    handler.handle(item);
                }
                datePickerDialog.dismiss();
            }
        });
        numberAdapter.setNewData(sourceList);
    }

    public void show(){
        datePickerDialog.show();
    }

}
